package com.dykj.fireFighting.server.base.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dykj.fireFighting.api.base.system.entity.SysDirectory;
import com.dykj.fireFighting.api.base.system.service.ISysDirectoryService;
import com.dykj.fireFighting.api.base.system.vo.SysDirectoryVo;
import com.dykj.fireFighting.common.core.service.impl.BaseServiceImpl;
import com.dykj.fireFighting.server.base.system.mapper.SysDirectoryMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @Author hxb
 * @Date 2021-06-19 15:12:17
 */
@Slf4j
@DubboService
@Transactional(rollbackFor = Exception.class)
public class SysDirectoryServiceImpl extends BaseServiceImpl<SysDirectoryMapper, SysDirectory> implements ISysDirectoryService {

    @Autowired
    private SysDirectoryMapper sysDirectoryMapper;


    @Override
    public List<SysDirectoryVo> querySysDirectory(Map<String, Object> par) {
        return sysDirectoryMapper.querySysDirectory(par);
    }

    @Override
    public List<SysDirectoryVo> queryDirectoryByType(List<String> typeList) {
        return sysDirectoryMapper.queryDirectoryByType(typeList);
    }

    /**
     * 递归查询子字典数据
     *
     * @param sysDirectoryList
     * @param code
     * @return
     */
    @Override
    public List<SysDirectory> getChildSysDirectoryList(List<SysDirectory> sysDirectoryList, String code) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("parentCode", code);
        List<SysDirectory> list = sysDirectoryMapper.getChildSysDirectoryList(params);
        for (SysDirectory sysDirectory : list) {
            sysDirectoryList.add(sysDirectory);
            getChildSysDirectoryList(sysDirectoryList, sysDirectory.getCode());
        }
        return sysDirectoryList;
    }

    @Override
    public List<SysDirectory> getByParentCode(String parentCode) {
        QueryWrapper<SysDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_code", parentCode)
                .eq("is_del", 0)
                .orderByAsc("sort");
        return sysDirectoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<SysDirectory> getParentDictTree(List<SysDirectory> sysDirectoryList, String code) {
        //查询数据库中对应id的实体类
        QueryWrapper<SysDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        SysDirectory sysDirectory = sysDirectoryMapper.selectOne(queryWrapper);
        sysDirectoryList.add(sysDirectory);
        //查询父级架构
        //此处使用mybaatisPlus的条件构造器，查询id等于pid的对象
        QueryWrapper<SysDirectory> queryParentWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", sysDirectory.getParentCode());
        SysDirectory parentSysDirectory = sysDirectoryMapper.selectOne(queryParentWrapper);
        if (parentSysDirectory != null && "-1".equals(parentSysDirectory.getParentCode())) {
            //如果查出的对象不为空，则将此对象的id存到全局变量中，并且继续调用自己，即递归，一直到查询不到为止
            getParentDictTree(sysDirectoryList, parentSysDirectory.getCode());
        }
        return sysDirectoryList;
    }

    @Override
    public int removeDirectoryById(String id) {
        SysDirectory sysDirectory = sysDirectoryMapper.selectById(id);
        QueryWrapper<SysDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_code", sysDirectory.getCode());
        int i = sysDirectoryMapper.selectCount(queryWrapper).intValue();
        if (i > 0) {
            return 0;
        }
        return sysDirectoryMapper.deleteById(id);
    }

    @Override
    public SysDirectoryVo getSysDirectoryVoById(Integer id) {
        return sysDirectoryMapper.getSysDirectoryVoById(id);
    }

    @Override
    public List<SysDirectory> queryDataByType(String dtyKeyCode) {
        QueryWrapper<SysDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dty_key_code", dtyKeyCode);
        queryWrapper.eq("is_del", 0);
        queryWrapper.lt("level", 3);
        return sysDirectoryMapper.selectList(queryWrapper);
    }

    @Override
    public SysDirectory queryByCode(String code) {
        QueryWrapper<SysDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code", code);
        return sysDirectoryMapper.selectOne(queryWrapper);
    }

    @Override
    public List<SysDirectory> queryParentDataByType(String dtyKeyCode) {
        QueryWrapper<SysDirectory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dty_key_code", dtyKeyCode);
        queryWrapper.eq("is_del", 0);
        queryWrapper.eq("parent_code", -1);
        return sysDirectoryMapper.selectList(queryWrapper);
    }

}
